package com.tywj.panda.driver.api

import android.os.Bundle
import android.view.Gravity
import android.view.WindowManager
import com.tywj.panda.customer.common.view.dialog.BottomDialog
import com.tywj.panda.driver.R
import com.tywj.lib.core.common.extension.onClick
import com.tywj.lib.core.common.util.LogUtil
import kotlinx.android.synthetic.main.driver_selected_date.*

/**
 * 选择日期
 */
class SelectedDateDialog : BottomDialog() {

    companion object {
        const val TAG = "SelectedDateDialog"
    }

    /**
     * 当前选择的时间
     */
    private var currentDate = ""

    private var onChoseListener: ((String) -> Unit)? = null

    override fun getGravity(): Int = Gravity.CENTER

    override fun getWidth(): Int = WindowManager.LayoutParams.WRAP_CONTENT

    override fun getLayoutID(): Int = R.layout.driver_selected_date

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
    }

    override fun initListener() {
        super.initListener()

        monthCalendar.setOnCalendarChangedListener { _, year, month, localDate, _ ->
            val dateTime = "$year 年 $month 月"

            localDate?.let {
                currentDate = it.toString()
            }
            calendarTitle.text = dateTime
            LogUtil.d("setOnCalendarChangedListener:" + year + "年" + month + "月" + "   当前页面选中 " + localDate)
        }


        //默认为今天的设置
        monthCalendar.setOnCalendarMultipleChangedListener { baseCalendar, year, month, currPagerCheckedList, totalCheckedList, dateChangeBehavior ->
//            tv_result.setText(year.toString() + "年" + month + "月" + " 当前页面选中 " + currPagerCheckedList.size + "个  总共选中" + totalCheckedList.size + "个")
//            Log.d(TAG, year.toString() + "年" + month + "月")
//            Log.d(TAG, "当前页面选中：：$currPagerCheckedList")
//            Log.d(TAG, "全部选中：：$totalCheckedList")
        }

        ivPreMonth.onClick {
            monthCalendar.toLastPager()
        }
        ivNextMonth.onClick {
            monthCalendar.toNextPager()
        }

        btnCancel.onClick {
            dismiss()
        }
        btnConfirm.onClick {
            onChoseListener?.invoke(currentDate)
            dismiss()
        }
    }

    fun setSelectedDateTime(listener: (time: String) -> Unit) {
        onChoseListener = listener
    }
}